package ywpt.service.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ywpt.components.acegi.domain.Function;
import ywpt.components.acegi.domain.Resource;
import ywpt.core.dao.extend.HibernateEntityExtendDao;
import ywpt.core.exception.UniqueConstraintException;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataIntegrityViolationException;

/**
 * 
 * 权限资源服务实现类 2007-10-8 下午09:57:23
 * 
 * @author <a href="mailto:mrwyxster@gmail.com">Oliver Wen</a>
 * 
 */
public class ResourceServiceImpl extends HibernateEntityExtendDao<Resource> implements ResourceService {
	public ResourceServiceImpl() {
	}

	public Resource getResourceById(Long id) {
		return this.get(Resource.class, id);
	}

	public List<Resource> getResources() {
		return this.getAll(Resource.class);
	}

	@SuppressWarnings("unchecked")
	public List<Resource> getResourcesByCriteria(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	public List<Resource> getResourcesByCriteria(DetachedCriteria query, int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}
	
	@SuppressWarnings("unchecked")
	public List<Resource> getResourcesByRole(String roleId) {
		String hql = "select res from ywpt.components.acegi.domain.Resource res left join res.roles role where role.id="+roleId;
		return this.find(hql);
	}
 
	public void saveResource(Resource resource) {
		try {
			this.save(resource);
		} catch (DataIntegrityViolationException e) {
			throw new UniqueConstraintException("resource '" + resource.getResName() + "' already exists!");
		}
	}

	public void updateResource(Resource resource) {
		this.update(resource);
	}
	
	public void removeResource(String id) {
		this.removeById(new Long(id));
	}
	
	public int[] removeResources(Long[] ids) {
		String hql = "delete ywpt.components.acegi.domain.Resource r where r.id =?";
		int results[] = new int[ids.length];
		for (int i = 0; i < ids.length; i++) {
			results[i] = super.bulkUpdate(hql, ids[i]);
		}
		return results;
	}

	public int getResCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	public List getFunctionResTree() {
		String hql = "from ywpt.components.acegi.domain.Function as f where f.functionCode is null";
		List<Function> functionList = super.find(hql);
		List<Map<String, Object>> jsonList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < functionList.size(); i++) {
			Function row = functionList.get(i);
			Map<String, Object> child = new HashMap<String, Object>();
			child.put("text", row.getFunctionName());
			child.put("id", row.getFunctionCode());
			List grandChildRow = null; // getResourceTree(row.getFunctionCode());
			if (grandChildRow.size() != 0) {
				child.put("children", grandChildRow);
			} else {
				child.put("leaf", Boolean.TRUE);
			}
			jsonList.add(child);
		}
		return jsonList;
	}
	
	@Override
	protected void onValid(Resource entity) {
		if (!isUnique(Resource.class, entity, "resName")) {
			throw new UniqueConstraintException("资源名" + entity.getResName() + "重复");
		}
	}
}